package com.suning.sawp.service.impl.tm.weixin;

import java.util.Date;

import org.apache.commons.lang3.RandomStringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.suning.rsc.dto.responsedto.MbfResponse;
import com.suning.rsc.httpservice.annotation.EsbEIHttpWired;
import com.suning.sawp.dto.esb.GetTokenInfoReq;
import com.suning.sawp.intf.esb.WechatGetWeChatTokenInfoService;
import com.suning.vgs.im.sp.dto.lightapp.GetTokenInfoResponse;

@Service
public class WeixinConfigService {

    private static final Logger LOGGER = LoggerFactory.getLogger(WeixinConfigService.class);

    private static final String JS_API_TICKET = "2";

    private static final String API_TICKET = "3";

    private static final String SUCCESS = "0";

    private static final String SHA1_PREFIX = "SNStoreSNweixin";

    private WechatGetWeChatTokenInfoService wechatGetWeChatTokenInfoService;

    public String getJsticket(String appId) {

        GetTokenInfoReq req = new GetTokenInfoReq();
        req.setAppId(appId);
        req.setAccessType(JS_API_TICKET);
        String timeStamp = System.currentTimeMillis() + "";
        req.setTimeStamp(timeStamp);
        String sourceStr = SHA1_PREFIX + timeStamp + appId;
        String sign = Digestes.getSHA1Signed(sourceStr);
        req.setSign(sign);

        GetTokenInfoResponse response = null;
        try {
            MbfResponse mbfResponse = wechatGetWeChatTokenInfoService.getWeChatTokenInfo(req);
            response = (GetTokenInfoResponse) mbfResponse.getOutput().getMbfBody(GetTokenInfoResponse.class);
        } catch (Exception e) {
            LOGGER.error("WeixinConfigService invoke wechatGetWeChatTokenInfoService error, params:{}", req.toString(),
                    e);
        }
        if (null != response && SUCCESS.equals(response.getStatus()) && null != response.getTokenInfo()) {
            return response.getTokenInfo().getToken();
        }
        LOGGER.error("WeixinConfigService getJsticket appId:{} error!", appId);
        return null;
    }

    public WeixinJsSdkConfig getWeixinJsSdkConfig(String appId, String jsApiTicket, String url) {
        // 随机字符串
        String nonceStr = RandomStringUtils.randomAlphanumeric(16);
        // 时间戳
        String timestamp = (new Date().getTime() / 1000) + "";
        // 需要做sha1签名的字符串
        String sourceStr = new StringBuilder("jsapi_ticket=").append(jsApiTicket).append("&noncestr=").append(nonceStr)
                .append("&timestamp=").append(timestamp).append("&url=").append(url).toString();
        String sign = Digestes.getSHA1Signed(sourceStr);

        WeixinJsSdkConfig config = new WeixinJsSdkConfig();
        config.setAppId(appId);
        config.setNonceStr(nonceStr);
        config.setSignature(sign);
        config.setTimestamp(timestamp);

        return config;
    }

    @EsbEIHttpWired
    public void setWechatGetWeChatTokenInfoService(WechatGetWeChatTokenInfoService wechatGetWeChatTokenInfoService) {
        this.wechatGetWeChatTokenInfoService = wechatGetWeChatTokenInfoService;
    }

    public WechatGetWeChatTokenInfoService getWechatGetWeChatTokenInfoService() {
        return wechatGetWeChatTokenInfoService;
    }

    public String getApiTicket(String appId) {

        GetTokenInfoReq req = new GetTokenInfoReq();
        req.setAppId(appId);
        req.setAccessType(API_TICKET);
        String timeStamp = System.currentTimeMillis() + "";
        req.setTimeStamp(timeStamp);
        String sourceStr = SHA1_PREFIX + timeStamp + appId;
        String sign = Digestes.getSHA1Signed(sourceStr);
        req.setSign(sign);

        GetTokenInfoResponse response = null;
        try {
            MbfResponse mbfResponse = wechatGetWeChatTokenInfoService.getWeChatTokenInfo(req);
            response = (GetTokenInfoResponse) mbfResponse.getOutput().getMbfBody(GetTokenInfoResponse.class);
        } catch (Exception e) {
            LOGGER.error("WeixinConfigService invoke wechatGetWeChatTokenInfoService error, params:{}", req.toString(),
                    e);
        }
        if (null != response && SUCCESS.equals(response.getStatus()) && null != response.getTokenInfo()) {
            return response.getTokenInfo().getToken();
        }
        LOGGER.error("WeixinConfigService getJsticket appId:{} error!", appId);
        return null;
    }
}
